<template>
  <el-dialog title="销售订单选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="80%"
    center
    >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="销售订单编码" prop="soNumber">
            <el-input
              v-model="queryParams.soNumber"
              placeholder="请输入销售订单编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="销售订单名称" prop="soName">
            <el-input
              v-model="queryParams.soName"
              placeholder="请输入销售订单名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="订单日期" prop="orderDate">
            <el-date-picker clearable
                            v-model="queryParams.orderDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择订单日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <el-table v-loading="loading" :data="orderList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
        <el-table-column width="50" align="center" >
            <template v-slot="scope">
                <el-radio v-model="selectedLineId" :label="scope.row.lineId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
            </template>
        </el-table-column>
      <el-table-column label="销售订单号" align="center" prop="soNumber" width="200px" />
      <el-table-column label="产品编码" align="center" prop="itemCode" width="200px" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="图号" align="center" prop="drawNumber" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="订单数量" align="center" prop="quantity" />
<!--      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '草稿'" effect="dark" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === '已审批'" effect="dark">已审批</el-tag>
          <el-tag v-else-if="scope.row.status === '已完成'" effect="dark" type="success">已完成</el-tag>
          <el-tag v-else-if="scope.row.status === '已退货'" effect="dark" type="danger">已退货</el-tag>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrder, listOrder, listStatusOrder } from "@/api/mes/xs/order";
import {getDetails} from "@/api/mes/xs/line"
export default {
  name: "OrderSelect",
  props: {
    status: null,
    status2: null,
    clientId:null,
  },
  data() {
    return {
      showFlag:false,
      selectedLineId:undefined,
      selectedOrderId: undefined,
      selectedRow: undefined,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNumber: null,
        soName: null,
        clientName: null,
        clientId: null,
        orderDate: null,
        status: null,
        virtual: null,
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    clientId: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.queryParams.clientId = newVal;
          this.getList();
        }
      },
      immediate: false
    }
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.selectedLineId=null
      this.selectedRow=null
      this.loading = true;
      this.queryParams.status = this.status
      this.queryParams.virtual = this.status2
      this.queryParams.clientId=this.clientId
      getDetails(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row){
        if(row){
            this.selectedRow = row;
        }
    },
    //行双击选中
    handleRowDbClick(row){
      if(row){
        this.selectedRow = row;
        this.$emit('onSelected',this.selectedRow);
        this.showFlag = false;
        this.selectedLineId=null
        this.selectedRow=null
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRow = row;
      }
    },
    cancel(){
      this.showFlag=false
      this.selectedLineId=null
      this.selectedRow=null
    },
    //确定选中
    confirmSelect(){
        if(this.selectedLineId == null || this.selectedLineId == 0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected',this.selectedRow);
        this.showFlag = false;
      this.selectedLineId=null
      this.selectedRow=null
    }
  }
};
</script>
